package cate.game.res;

import cate.common.table.d.GDOperation;
import cate.game.client.msg.MergeMsgSender;
import cate.game.mail.po.MailBuilder;
import cate.game.role.Role;
import easy.java.practice.errorcode.EcResult;

/**
 * MixRes添加器
 */
public class MixResAdder {

    private MixRes res;

    public MixResAdder setRes(MixRes res) {
        this.res = res;
        return this;
    }

    public MixResAdder setRes(String resConf) {
        this.res = new MixRes(resConf);
        return this;
    }

    private int operation;

    public MixResAdder setOperation(int operation) {
        this.operation = operation;
        return this;
    }

    private int operationSub;

    public MixResAdder setOperationSub(int operationSub) {
        this.operationSub = operationSub;
        return this;
    }

    private boolean sendMailWhenFull = true;

    public MixResAdder setSendMailWhenFull(boolean sendMailWhenFull) {
        this.sendMailWhenFull = sendMailWhenFull;
        return this;
    }

    private MergeMsgSender sender;

    public MixResAdder setMsgSending(MergeMsgSender sender) {
        this.sender = sender;
        return this;
    }

    private boolean dialogReward;

    public MixResAdder setDialogReward(boolean dialogReward) {
        this.dialogReward = dialogReward;
        return this;
    }

    /**
     * 执行添加
     */
    public EcResult exe(Role role) {
        EcResult r;
        if (res == null) {
            r = new EcResult();
            r.fail("添加资源为NULL");
            return r;
        }
        r = res.addCheck(role);
        if (r.ok()) {
            boolean needSet = operation > 0 && role.log.getOperation() == 0;
            if(needSet){
                role.log.setOperation(operation);
                role.log.setOperationSub(operationSub);
            }
            for (MixResItem item : res.items) {
                item.addDirect(role, sender);
            }
            if (dialogReward) {
                role.getGame().notice.dialogReward(role, res);
            }
            if(needSet){
                role.log.clearOperation();
            }
            return r;
        } else {
            if (sendMailWhenFull) {
                String content = "由于您的宝物实在太多了，包裹实在是放不下啦！在 %s 里散落一地的宝物洛依依已经帮您带回来了。";
//                // 发送邮件
                int op = operation == 0 ? role.log.getOperation() : operation;
                MailBuilder mb =
                        new MailBuilder()
                                .setTitle("获得物品")
                                .setContent(String.format(content, GDOperation.getDesc(op, 0)))
                                .setReward(res)
                                .setOperation(op)
                                .setOperationSub(operationSub == 0 ? role.log.getOperationSub() : operationSub);
                role.getMail().add(mb.build());
                role.getGame().notice.message(role, "背包已满，已转发至邮件");
                return r.success("以邮件形式发送了奖励");
            } else {
                return r;
            }
        }
    }
}
